﻿using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Update
{
    public partial class Form1 : Form
    {

        private MySqlConnection myconn =
            new MySqlConnection("Host = localhost; Database = test; Username = root; Password = root");

        private int _radio = 0;
        private Form2 another;

        public Form1()
        {
            InitializeComponent();
            add_head();
           
        }

        private void add_head()
        {
            //http://www.cnblogs.com/esion/archive/2013/07/31/3227023.html
            //this.listView1.Columns.Add("勾选", 120, HorizontalAlignment.Center);
            this.listView1.Columns.Add("邮件编号", 60, HorizontalAlignment.Center);
            this.listView1.Columns.Add("收件人", 50, HorizontalAlignment.Center);
            this.listView1.Columns.Add("收件人地址", 120, HorizontalAlignment.Center);
            this.listView1.Columns.Add("收件人联系方式", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("发件人", 60, HorizontalAlignment.Center);
            this.listView1.Columns.Add("发件人地址", 120, HorizontalAlignment.Center);
            this.listView1.Columns.Add("发件人联系方式", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("到件时间", 80, HorizontalAlignment.Center);
            //this.listView1.Columns.Add("到件时间", 80, HorizontalAlignment.Center);
        }

        private void load(String sql)
        {
            listView1.Items.Clear();
            this.listView1.BeginUpdate();
            myconn.Open();
            MySqlCommand mycom = myconn.CreateCommand();
            mycom.CommandText = sql;
            mycom.CommandType = CommandType.Text;
            MySqlDataReader sdr = mycom.ExecuteReader();
            int index = 0;
            while (sdr.Read())
            {
                listView1.Items.Add("");
                listView1.Items[index].SubItems.Add(sdr["mail_id"].ToString());
                listView1.Items[index].SubItems.Add(sdr["receive_name"].ToString());
                listView1.Items[index].SubItems.Add(sdr["receive_address"].ToString());
                listView1.Items[index].SubItems.Add(sdr["receive_tel"].ToString());
                listView1.Items[index].SubItems.Add(sdr["post_name"].ToString());
                listView1.Items[index].SubItems.Add(sdr["post_address"].ToString());
                listView1.Items[index].SubItems.Add(sdr["post_tel"].ToString());
                listView1.Items[index].SubItems.Add(sdr["arrive_time"].ToString());
                ++index;
            }
            myconn.Close();
            this.listView1.EndUpdate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                item.Checked = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                item.Checked = false;
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            load("select * from mail where mail_state=0");
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.CheckedItems)
            {
                string sql = "update mail set mail_state=1 where mail_id=" + item.SubItems[1].Text;
                Change(sql);
            }
            load("select * from mail where mail_state=0");
        }

        private void Change(string sql)
        {
            myconn.Open();
            MySqlCommand cmd = new MySqlCommand(sql, myconn);
            cmd.ExecuteNonQuery();
            myconn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.CheckedItems)
            {
                string sql = "update mail set mail_state=2 where mail_id=" + item.SubItems[1].Text;
                Change(sql);
            }
            load("select * from mail where mail_state="+_radio);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.CheckedItems)
            {
                item.Checked = false;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            load("select * from mail where mail_state=0");
            _radio = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            load("select * from mail where mail_state=1");
            _radio = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            load("select * from mail where mail_state=2");
            _radio = 2;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.CheckedItems)
            {
                string sql = "update mail set mail_state=0 where mail_id=" + item.SubItems[1].Text;
                Change(sql);
            }
            load("select * from mail where mail_state=" + _radio);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            another = new Form2();
            this.Hide();
            another.Show();
        }
    }
}
